<template>
	<view class="index">
		<view class="add">
			<image v-if="imgList.length>0" v-for="(items,index) in imgList" :src="items.path" :key="index" @click="openImg(items)"></image>
		</view>
		<span @click="addImage()">+</span>
	</view>
</template>
<script>
	import h5Copy from '@/js_sdk/junyi-h5-copy.js';
	export default {
		data() {
			return {
				// 初始化数据
				imgList: []
			}
		},
		onLoad() {
			// 获取页面的数据
			this.loadData();
		},
		methods: {
			addImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: async res => {
						if (res.tempFilePaths.length > 0) {
							// 上传显示
							uni.showLoading();
							let filePath = res.tempFilePaths[0];

							// 获取当前时间的毫秒数
							// let time = Date.now();

							// 获取文件的名称
							let fileNames = res.tempFiles[0].name;

							// 获取文件的后缀名
							let suffix = fileNames.substring(fileNames.lastIndexOf("."));

							let result = await uniCloud.uploadFile({
								cloudPath: `${Date.now()+suffix}`,
								filePath
							});
							// 将图片地址存入云数据库
							let addResult = await uniCloud.callFunction({
								name: "unitest",
								data: {
									method: "add",
									path: result.fileID
								}
							});
							uni.hideLoading();
							uni.showToast({
								title: "上传完成了",
								icon: "none"
							});
							// 每次上传完后都调用加载图片这个方法
							this.loadData();
						};
					}
				})
			},
			async loadData() {
				let queryData = await uniCloud.callFunction({
					name: "unitest",
					data: {
						method: "query"
					}
				});
				this.imgList = queryData.result.data;
			},
			async openImg(img) {
				uni.showModal({
					title: "图片操作",
					content: img.path,
					cancelText: "删除",
					confirmText: "复制",
					success: async res => {
						if (res.confirm) {
							// 复制地址
							// #ifdef H5
							let result = h5Copy(img.path);
							if (result === false) {
								uni.showToast({
									title: '不支持',
								})
							} else {
								uni.showToast({
									title: '复制成功',
									icon: 'none'
								})
							}
							// #endif

							// #ifdef APP-PLUS
							uni.setClipboardData({
								data: img.path,
								success: res => {
									uni.showToast({
										title: "复制成功",
										icon: "none"
									})
								}
							})
							// #endif

						} else {
							// 删除图片
							let delres = await uniCloud.callFunction({
								name: "unitest",
								data: {
									id: img._id,
									path: img.path,
									method: "del"
								}
							});

							if (delres.result === true) {
								uni.showToast({
									title: "删除成功",
									icon: "none"
								});
								// 删除成功后再重新调用数据；
								this.loadData();
							} else {
								uni.showToast({
									title: "删除失败",
									icon: "none"
								});
							};

						};
					}
				})
			},
		}
	}
</script>
<style lang="scss">
	page {
		height: 100%;
		width: 100%;
	}

	.index {
		padding: 0 20rpx;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .05);
	}

	.add {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		padding-top: 20rpx;

		>image {
			width: 150rpx;
			height: 150rpx;
			margin-top: 20rpx;
			margin-right: 20rpx;
			border-radius: 6rpx;
		}
	}

	span {
		position: absolute;
		right: 0;
		bottom: 0;
		width: 100rpx;
		height: 100rpx;
		background-color: pink;
		text-align: center;
		line-height: 100rpx;
		font-size: 60rpx;
	}
</style>
